package com.kele.service;

import com.kele.client.BrandFeignClient;
import com.kele.client.CategoryFeignClient;
import com.kele.client.GoodsFeignClient;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.model.goods.Brand;
import com.kele.model.goods.Category;
import com.kele.model.goods.Goods;
import com.kele.model.goods.GoodsImage;
import com.kele.model.goods.dto.GoodsDTO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import javax.servlet.http.HttpServletResponse;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {

    @Value("${ftl.directory}")
    String ftlDirectory;

    @Autowired
    GoodsFeignClient goodsFeignClient;

    @Autowired
    BrandFeignClient brandFeignClient;

    @Autowired
    CategoryFeignClient categoryFeignClient;

    @Autowired
    FreeMarkerConfig freeMarkerConfig;
    
    public Result genHtml(Long goodsId) throws IOException, TemplateException {
        Configuration configuration = freeMarkerConfig.getConfiguration();

        Template template = configuration.getTemplate("item.ftl");

        GoodsDTO goodsDTO = goodsFeignClient.selectById(goodsId);

        Goods goods = goodsDTO.getGoods();
        List<GoodsImage> goodsImageList = goodsDTO.getGoodsImageList();
        Map model = new HashMap<>();
        model.put("goods",goods);
        model.put("goodsImageList",goodsImageList);

        Category category1 = categoryFeignClient.findById(goods.getCategory1Id());
        Category category2 = categoryFeignClient.findById(goods.getCategory2Id());
        Category category3 = categoryFeignClient.findById(goods.getCategory3Id());

        model.put("category1Name",category1.getName());
        model.put("category2Name",category2.getName());
        model.put("category3Name",category3.getName());
        Writer html_out = new FileWriter(ftlDirectory + "" + goodsId.toString()+ ".html");
//      第七步：调用模板对象的 process 方法输出文件。
        template.process(model,html_out);//模板 template+ 数据  model =HTML(out)
        html_out.close();
        return Result.SUCCESS();
    }

    public Result genGoodsListHtml_makeForOrder(HttpServletResponse response) throws IOException, TemplateException {
        Configuration configuration = freeMarkerConfig.getConfiguration();

        Template template = configuration.getTemplate("goodsList_outofbox.ftl");

        PageResult<Goods> page = goodsFeignClient.findPage(1, 100, new HashMap());

        List<Goods> goodsList = page.getRecords();

        Map model = new HashMap<>();
        model.put("goodsList",goodsList);
        Map<String,String> brandMap = new HashMap();

        List<Brand> brandList = brandFeignClient.findAll();
        List<Category> categoryList = categoryFeignClient.findAll();
        brandList.forEach((brand)->{
            brandMap.put(brand.getId().toString(),brand.getName());
        });
        Map<String,String> categoryMap = new HashMap();
        categoryList.forEach((category)->{
            categoryMap.put(category.getId().toString(),category.getName());
        });

        model.put("brandMap",brandMap);
        model.put("categoryMap",categoryMap);

        response.setCharacterEncoding("utf-8");
        //response.setContentType("application/xml");
        PrintWriter writer = response.getWriter();
//      第七步：调用模板对象的 process 方法输出文件。
        template.process(model,writer);//模板 template+ 数据  model =HTML(out)

       /* Map map = new HashMap();
        map.put("name","zhangs3");
        map.put("age","23");
        map.put("status",response.getStatus());
        writer.print(JSON.toJSONString(map));*/

        writer.flush();
        writer.close();
        return Result.SUCCESS();
    }

    public Result genGoodsListHtml_outOfBox() throws IOException, TemplateException {
        Configuration configuration = freeMarkerConfig.getConfiguration();

        Template template = configuration.getTemplate("goodsList_outofbox.ftl");

        PageResult<Goods> page = goodsFeignClient.findPage(1, 100, new HashMap());

        List<Goods> goodsList = page.getRecords();

        Map model = new HashMap<>();
        model.put("goodsList",goodsList);
        Map<String,String> brandMap = new HashMap();

        List<Brand> brandList = brandFeignClient.findAll();
        List<Category> categoryList = categoryFeignClient.findAll();
        brandList.forEach((brand)->{
            brandMap.put(brand.getId().toString(),brand.getName());
        });
        Map<String,String> categoryMap = new HashMap();
        categoryList.forEach((category)->{
            categoryMap.put(category.getId().toString(),category.getName());
        });

        model.put("brandMap",brandMap);
        model.put("categoryMap",categoryMap);

        Writer html_out = new FileWriter(ftlDirectory +  "goodsList_outofbox.html");
//      第七步：调用模板对象的 process 方法输出文件。
        template.process(model,html_out);//模板 template+ 数据  model =HTML(out)
        html_out.close();
        return Result.SUCCESS();

    }

    public Result genGoodsListHtml_assembly() throws IOException, TemplateException {
        Configuration configuration = freeMarkerConfig.getConfiguration();

        Template template = configuration.getTemplate("goodsList_vue_assembling.ftl");

        PageResult<Goods> page = goodsFeignClient.findPage(1, 100, new HashMap());

        List<Goods> goodsList = page.getRecords();

        Map model = new HashMap<>();
        model.put("goodsList",goodsList);
        Map<String,String> brandMap = new HashMap();

        List<Brand> brandList = brandFeignClient.findAll();
        List<Category> categoryList = categoryFeignClient.findAll();
        brandList.forEach((brand)->{
            brandMap.put(brand.getId().toString(),brand.getName());
        });
        Map<String,String> categoryMap = new HashMap();
        categoryList.forEach((category)->{
            categoryMap.put(category.getId().toString(),category.getName());
        });

        model.put("brandMap",brandMap);
        model.put("categoryMap",categoryMap);

        Writer html_out = new FileWriter(ftlDirectory +  "goodsList_vue_assembling.html");
//      第七步：调用模板对象的 process 方法输出文件。
        template.process(model,html_out);//模板 template+ 数据  model =HTML(out)
        html_out.close();
        return Result.SUCCESS();

    }

    public Result genGoodsListHtml_afterSale(Integer pageNo, Integer pageSize) throws IOException, TemplateException {
        Configuration configuration = freeMarkerConfig.getConfiguration();

        Template template = configuration.getTemplate("goodsList_vue_aftersale.ftl");

        PageResult<Goods> page = goodsFeignClient.findPage(pageNo, pageSize, new HashMap());

        List<Goods> goodsList = page.getRecords();

        Map model = new HashMap<>();
        model.put("goodsList",goodsList);
        model.put("total",page.getTotal());
        Map<String,String> brandMap = new HashMap();

        List<Brand> brandList = brandFeignClient.findAll();
        List<Category> categoryList = categoryFeignClient.findAll();
        brandList.forEach((brand)->{
            brandMap.put(brand.getId().toString(),brand.getName());
        });

        model.put("brandMap",brandMap);

        Writer html_out = new FileWriter(ftlDirectory +  "goodsList_vue_aftersale.html");
//      第七步：调用模板对象的 process 方法输出文件。
        template.process(model,html_out);//模板 template+ 数据  model =HTML(out)
        html_out.close();
        return Result.SUCCESS();
    }


}
